//
//  XMStatusRetwwtView.swift
//  XMWeimo
//
//  Created by ios001 on 16/7/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class XMStatusRetwwtView: UIView {
    
    // 记录转发微博的底部约束
    var retwwtViewBottomConstraint: Constraint?
    
    // 供外界赋值
    var statusViewModel: XMStatusViewModel?{
        didSet{
//            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            contentLabel.attributedText = statusViewModel?.retweetContentAttr
            
            // 卸载约束
            retwwtViewBottomConstraint?.uninstall()
            // 代表有配图
            if statusViewModel?.status?.retweeted_status?.pic_urls?.count > 0 {
                // 赋值
                pictureView.pictureInfo = statusViewModel?.status?.retweeted_status?.pic_urls

                self.snp_makeConstraints { (make) -> Void in
                    self.retwwtViewBottomConstraint = make.bottom.equalTo(pictureView).offset(statusCellMargin).constraint
                }
                // 显示配图
                pictureView.hidden = false
            }else{
                // 代表没有配图
                // 更改约束
//                self.snp_makeConstraints(closure: { (make) -> Void in
//                    self.retwwtViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
//                })
                self.snp_makeConstraints { (make) -> Void in
                    self.retwwtViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(statusCellMargin).constraint
                }
                // 显示配图
                pictureView.hidden = true
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        // 01 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 02 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(statusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
            make.leading.equalTo((SCREENW - (cellWH*3 + cellMargin*2))/2)
        }
        // 约束当前的view
        self.snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(contentLabel).offset(10)
            self.retwwtViewBottomConstraint = make.bottom.equalTo(pictureView).offset(statusCellMargin).constraint
        }
        // 测试数据
    }
    
    // MARK: - 懒加载控件
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth: SCREENW - 20)
    // 配图
    private lazy var pictureView: XMStatusPictureView = XMStatusPictureView()
}
